<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
      <!-- {{ this.$route.params.kw }} -->
      <!-- 搜索结果 -->
      <div>
        <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
            :immediate-check="false"
        >
          <ArticleItem
          v-for="item in articleList" :key="item.art_id"
          :objItem = item
          :isShow = isShow
          @click.native="itemClickFN(item.art_id)"
          ></ArticleItem>
        </van-list>
      </div>
    </div>
  </div>
</template>

<script>
// 事件修饰符.native-> 给组件内根标签绑定原生click
import { searchResultAPI } from '@/api/index.js'
import ArticleItem from '@/components/ArticleItem'

export default {
  name: 'SearchResult',
  data() {
    return {
      page: 1,
      per_page: 10,
      articleList: [],
      isShow: false, // 控制x的显示
      loading: false, // 加载状态
      finished: false // 是否全部加载完成
    }
  },
  methods: {
    async searchResult() {
      const { data: res } = await searchResultAPI({
        page: this.page,
        per_page: this.per_page,
        q: this.$route.params.kw
      })
      if (res.data.results.length === 0) {
        this.finished = true
      }
      // console.log(res)
      this.articleList = [...this.articleList, ...res.data.results]
    },
    onLoad() {
      if (this.articleList.length > 0) {
        this.page++
        this.searchResult() // 再次调用接口
        this.loading = false
      }
    },
    // 路由跳转到详情页面
    itemClickFN(id) {
      this.$router.push({
        path: `/article_detail?art_id=${id}`
      })
    }
  },
  created() {
    this.searchResult()
  },
  components: {
    ArticleItem
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
padding-top: 46px;
}
</style>
